home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / pyshared / checkbox / properties.py < prev    next >
Text File  |  2009-11-05  |  6KB  |  200 lines

  1. #
  2. # This file is part of Checkbox.
  3. #
  4. # Copyright 2008 Canonical Ltd.
  5. #
  6. # Checkbox is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # Checkbox is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Checkbox.  If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. from checkbox.attribute import Attribute
  20. from checkbox.variables import (ConstantVariable, BoolVariable, StringVariable,
  21.     PathVariable, UnicodeVariable, IntVariable, FloatVariable, ListVariable,
  22.     TupleVariable, AnyVariable, DictVariable, MapVariable, VariableFactory,
  23.     Variable, get_variable)
  24.  
  25.  
  26. class Property(object):
  27.  
  28.     def __init__(self, variable_class=Variable, variable_kwargs={}):
  29.         self._variable_class = variable_class
  30.         self._variable_kwargs = variable_kwargs
  31.  
  32.     def __get__(self, obj, cls=None):
  33.         if obj is None:
  34.             return self._get_attribute(cls)
  35.         if cls is None:
  36.             cls = type(obj)
  37.         attribute = self._get_attribute(cls)
  38.         variable = get_variable(obj, attribute)
  39.         return variable.get()
  40.  
  41.     def __set__(self, obj, value):
  42.         cls = type(obj)
  43.         attribute = self._get_attribute(cls)
  44.         variable = get_variable(obj, attribute)
  45.         variable.set(value)
  46.  
  47.     def _detect_name(self, used_cls):
  48.         self_id = id(self)
  49.         for cls in used_cls.__mro__:
  50.             for attr, prop in cls.__dict__.items():
  51.                 if id(prop) == self_id:
  52.                     return attr
  53.         raise RuntimeError("Property used in an unknown class")
  54.  
  55.     def _get_attribute(self, cls):
  56.         try:
  57.             attribute = cls.__dict__["_checkbox_attributes"].get(self)
  58.         except KeyError:
  59.             cls._checkbox_attributes = {}
  60.             attribute = None
  61.  
  62.         if attribute is None:
  63.             name = self._detect_name(cls)
  64.             attribute = PropertyAttribute(self, cls, name,
  65.                 self._variable_class, self._variable_kwargs)
  66.             cls._checkbox_attributes[self] = attribute
  67.  
  68.         return attribute
  69.  
  70.     def coerce(self, value):
  71.         return self._variable_class(**self._variable_kwargs).coerce(value)
  72.  
  73.  
  74. class PropertyAttribute(Attribute):
  75.  
  76.     def __init__(self, prop, cls, name, variable_class, variable_kwargs):
  77.         super(PropertyAttribute, self).__init__(name, cls,
  78.             VariableFactory(variable_class, attribute=self, **variable_kwargs))
  79.  
  80.         self.cls = cls # Used by references
  81.  
  82.         # Copy attributes from the property to avoid one additional
  83.         # function call on each access.
  84.         for attr in ["__get__", "__set__"]:
  85.             setattr(self, attr, getattr(prop, attr))
  86.  
  87.  
  88. class PropertyType(Property):
  89.  
  90.     def __init__(self, **kwargs):
  91.         kwargs["value"] = kwargs.pop("default", None)
  92.         kwargs["value_factory"] = kwargs.pop("default_factory", None)
  93.         super(PropertyType, self).__init__(self.variable_class, kwargs)
  94.  
  95.  
  96. class PropertyFactory(PropertyType):
  97.  
  98.     def __init__(self, type=None, **kwargs):
  99.         if "default" in kwargs:
  100.             raise ValueError("'default' not allowed for factories. "
  101.                              "Use 'default_factory' instead.")
  102.         if type is None:
  103.             type = Property()
  104.         kwargs["item_factory"] = VariableFactory(type._variable_class,
  105.             **type._variable_kwargs)
  106.         super(PropertyFactory, self).__init__(**kwargs)
  107.  
  108.  
  109. class Constant(PropertyFactory):
  110.  
  111.     variable_class = ConstantVariable
  112.  
  113.  
  114. class Bool(PropertyType):
  115.  
  116.     variable_class = BoolVariable
  117.  
  118.  
  119. class String(PropertyType):
  120.  
  121.     variable_class = StringVariable
  122.  
  123.  
  124. class Path(PropertyType):
  125.  
  126.     variable_class = PathVariable
  127.  
  128.  
  129. class Unicode(PropertyType):
  130.  
  131.     variable_class = UnicodeVariable
  132.  
  133.  
  134. class Int(PropertyType):
  135.  
  136.     variable_class = IntVariable
  137.  
  138.  
  139. class Float(PropertyType):
  140.  
  141.     variable_class = FloatVariable
  142.  
  143.  
  144. class List(PropertyFactory):
  145.  
  146.     variable_class = ListVariable
  147.  
  148.     def __init__(self, *args, **kwargs):
  149.         kwargs["separator"] = kwargs.pop("separator", r"\s")
  150.         super(List, self).__init__(*args, **kwargs)
  151.  
  152.  
  153. class Tuple(PropertyFactory):
  154.  
  155.     variable_class = TupleVariable
  156.  
  157.  
  158. class Any(PropertyType):
  159.  
  160.     variable_class = AnyVariable
  161.  
  162.     def __init__(self, schemas=[], **kwargs):
  163.         kwargs["schemas"] = [VariableFactory(schema._variable_class,
  164.             **schema._variable_kwargs) for schema in schemas]
  165.         self.schemas = schemas
  166.         super(Any, self).__init__(**kwargs)
  167.  
  168.  
  169. class Dict(PropertyType):
  170.  
  171.     variable_class = DictVariable
  172.  
  173.     def __init__(self, key, value, **kwargs):
  174.         kwargs["key_schema"] = VariableFactory(key._variable_class,
  175.             **key._variable_kwargs)
  176.         kwargs["value_schema"] = VariableFactory(value._variable_class,
  177.             **value._variable_kwargs)
  178.         super(Dict, self).__init__(**kwargs)
  179.  
  180.  
  181. class Map(PropertyType):
  182.  
  183.     variable_class = MapVariable
  184.  
  185.     def __init__(self, schema={}, **kwargs):
  186.         for key, type in schema.items():
  187.             schema[key] = VariableFactory(type._variable_class,
  188.                 **type._variable_kwargs)
  189.  
  190.         kwargs["schema"] = schema
  191.         super(Map, self).__init__(**kwargs)
  192.  
  193.  
  194. class Message(Map):
  195.  
  196.     def __init__(self, type, schema={}, **kwargs):
  197.         self.type = type
  198.         schema["type"] = Constant(default_factory=lambda: type, type=String())
  199.         super(Message, self).__init__(schema, **kwargs)
  200.